package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.entity.SetmealDish;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    private DishFlavorService dishFlavorService;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 新增菜品和口味
     * @param dishDto
     */
    @Transactional
    public void saveWithFlavor(DishDto dishDto) {
        save(dishDto);
        Long id = dishDto.getId();
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map((DishFlavor flavor) -> {
            flavor.setDishId(id);
            return flavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(collect);
    }

    @Override
    public Page<DishDto> pageByName(Page<DishDto> page, String name) {
        return dishMapper.pageByName(page,name);
    }

    @Override
    public DishDto getByIdWithFlavor(Long id) {
        Dish dish = getById(id);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(dish.getId() != null,DishFlavor::getDishId,dish.getId());
        List<DishFlavor> flavors = dishFlavorService.list(lqw);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish,dishDto);
        dishDto.setFlavors(flavors);
        return dishDto;
    }

    @Override
    @Transactional
    public void updateWithFlavor(DishDto dishDto) {
        updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> lqw = new LambdaQueryWrapper<>();
        lqw.eq(DishFlavor::getDishId,dishDto.getId());
        dishFlavorService.remove(lqw);
        List<DishFlavor> flavors = dishDto.getFlavors();
        List<DishFlavor> collect = flavors.stream().map((DishFlavor flavor) -> {
            flavor.setDishId(dishDto.getId());
            return flavor;
        }).collect(Collectors.toList());
        dishFlavorService.saveBatch(collect);
    }

    @Override
    public void updateStatusById(int status,List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId,ids);
        if (status == 0){
            //判断关联的套餐是否启售
            LambdaQueryWrapper<SetmealDish> setmealDishLqw = new LambdaQueryWrapper<>();
            setmealDishLqw.in(SetmealDish::getDishId,ids);
            List<SetmealDish> setmealDishes = setmealDishService.list(setmealDishLqw);
            //套餐id的集合
            List<Long> setmealIds = setmealDishes.stream().map(item -> {
                return item.getSetmealId();
            }).distinct().collect(Collectors.toList());
            if (setmealIds.size() > 0){
                LambdaQueryWrapper<Setmeal> setmealLqw = new LambdaQueryWrapper<>();
                setmealLqw.eq(Setmeal::getStatus,1);
                setmealLqw.in(Setmeal::getId,setmealIds);
                int count = setmealService.count(setmealLqw);
                if (count > 0){
                    throw new CustomException("选中的菜品中有的关联套餐是启售状态，不能停售该");
                }
            }
            lqw.eq(Dish::getStatus,1);
        } else {
            lqw.eq(Dish::getStatus,0);
        }
        int count = count(lqw);
        //判断是否都是禁用或启用
        if (count != ids.size()){
            if (status == 0){
                throw new CustomException("选中的已有禁用的");
            } else {
                throw new CustomException("选中的已有启用的");
            }
        }
        List<Dish> dishes = listByIds(ids);
        List<Dish> collect = dishes.stream().map(item -> {
            item.setStatus(status);
            return item;
        }).collect(Collectors.toList());
        updateBatchById(collect);
    }

    @Override
    @Transactional
    public void delete(List<Long> ids) {
        LambdaQueryWrapper<Dish> lqw = new LambdaQueryWrapper<>();
        lqw.in(Dish::getId, ids);
        lqw.eq(Dish::getStatus, 1);
        int count = count(lqw);
        if (count > 0) {
            throw new CustomException("该菜品是启售状态，不能删除");
        }
        LambdaQueryWrapper<SetmealDish> setmealDishLqw = new LambdaQueryWrapper<>();
        setmealDishLqw.in(SetmealDish::getDishId, ids);
        int setmealDishCount = setmealDishService.count(setmealDishLqw);
        if (setmealDishCount > 0) {
            throw new CustomException("选中的已有关联套餐，不能删除");
        }
        removeByIds(ids);
        LambdaQueryWrapper<DishFlavor> dishFlavorLqw = new LambdaQueryWrapper<>();
        dishFlavorLqw.in(DishFlavor::getDishId,ids);
        List<DishFlavor> list = dishFlavorService.list(dishFlavorLqw);
        List<Long> collect = list.stream().map(item -> item.getId()).distinct().collect(Collectors.toList());
        dishFlavorService.removeByIds(collect);
    }
}
